<?php 

$this->headScript()->appendFile($this->baseUrl().'/public/js/jquery-ui-1.8.custom.min.js' ); 

if( $this->isPost ) {

	if( ( $this->sort == 'price' ) && ( !$this->emptyResult ) ) {
		$list = array();
		foreach( $this->list  as $value ) {
			
			$free = $this->Findfree( $value['Dimension'] );
			
			$list[$this->Price( $value, $free, $this->locale ). '.' .$value['p_id']] = $value;
			
		}
		if( $this->sort_direction == 'desc' ) {
			krsort( $list );
		} else {
			ksort( $list );
			
		}
	} else {
		$list = $this->list;
	}
	$pager = true;
} else {
	
	$list = array();
	$pager = false;
}
?>

<!--[if lte IE 8.0]>
<script type="text/javascript" src="{baseUrl}/public/js/fix-zindex.js"></script>
<![endif]-->


<?php if( $this->adv ) { ?>
<div class="box nomtop">
	
	<h1><?php echo $this->translate('advanced_search');?></h1>
	
	<form id="sfrom" action="" method="get">
	<table class="tab">
		<tr>
			<td class="aright"><?php echo $this->translate( 'search_query' ); ?>:</td>
			<td><input type="text" name="q" value="<?php echo $this->q; ?>"></td>
		</tr>
		<tr>
			<td class="aright"><?php echo $this->translate( 'prince_range' ) ?>:</td>
			<td>
			<?php echo $this->translate( 'price_from' ) ?> <input type="text" name="price_from" value="<?php echo $this->price_from ?>" style="width:50px"> 
			<?php echo $this->translate( 'price_to' ) ?> <input type="text" name="price_to" value="<?php echo $this->price_to ?>" style="width:50px">
			</td>
		</tr>
		
			
		<tr>
			<td class="aright"><?php echo $this->translate( 'search_categories' ); ?>:</td>
			<td>
					
					<table>
						<tr>
    						<td>
							<?php $i = 1; foreach( $this->categories as $value ) { ?>
								<?php if( $i % 2 ) { ?>
								<input <?php echo in_array( $value['c_id'], $this->categoriesFiltr ) ? 'checked="checked"' : ''; ?> type="checkbox" name="c[]" value="<?php echo $value['c_id'] ?>" id="c<?php echo $value['c_id'] ?>"><label for="c<?php echo $value['c_id'] ?>"><?php echo $value['name_' . $this->locale] ?></label><br /> 
								<?php } ?>
							<?php $i++; } ?>
							</td>
							<td>
							<?php $i = 0; foreach( $this->categories as $value ) { ?>
								<?php if( $i % 2 ) { ?>
								<input <?php echo in_array( $value['c_id'], $this->categoriesFiltr ) ? 'checked="checked"' : ''; ?> type="checkbox" name="c[]" value="<?php echo $value['c_id'] ?>" id="c<?php echo $value['c_id'] ?>"><label for="c<?php echo $value['c_id'] ?>"><?php echo $value['name_' . $this->locale] ?></label><br /> 
								<?php } ?>
							<?php $i++; } ?>
							</td>
						</tr>
					</table>
					
			</td>
		</tr>
		<tr>
			<td class="aright"><?php echo $this->translate( 'search_manufacturers' ) ?>:</td>
			<td>
				<select name="m" multiple="multiple" size="8" style="width:300px">
				<?php foreach( $this->manufacturers as $value ) { ?>
    				<option <?php echo in_array( $value['m_id'], $this->manufacturesFiltr ) ? 'selected="selected"' : ''; ?>  value="<?php echo $value['m_id'] ?>" id="m<?php echo $value['m_id'] ?>"><?php echo $value['name_' . $this->locale] ?></option>
    			<?php }?>
				</select>
			</td>
		</tr>
	</table>
	</form>
	
	<div class="line" style="margin:10px 0"></div>
	
	<div class="acenter">
	<a href="javascript:;" class="button" onclick="$('#sfrom').submit()">Szukaj</a>
	</div>
	
	<div class="end"></div>
</div>
<!-- end box -->
<?php } ?>


<?php if( $this->emptyResult ) { ?>

<!-- NIE MA WYNIKÓW WYSZUKIWANIA -->

	<div class="content mtop5">
		<?php echo $this->translate( 'products_empty_result' ); ?>
	</div>
<?php } else { ?>
		
	<?php 
	    // Tablica z kategoriami ktore zostaly zaznaczone (id kategorii, name)
	    $categoriesSel = array();
	    
	    if( $this->categories ) {
	    
		    foreach( $this->categories as $value ) 
		    {		
	    		if(in_array( $value['c_id'], $this->categoriesFiltr ))
	    		{
	    		    $categoriesSel[] = array(
	    		    			'name' =>$value['name_' . $this->locale], 
	    		    			'id' => $value['c_id']);
	            }
	        }
        
	    }
        
    ?>



<?php  if(!empty($list)) { ?>

<div class="sorting" style="margin:10px 0 0 0 ">
	<div class="text">
	
		
		<!-- start kategorie -->
		
		<div style="position:absolute; right:10px; top:0px">
		<?php 
			if( isSet( $categoriesSel[0]['id'] ) ) {
			    echo '<span id="catclose'.$categoriesSel[0]['id'].'">'.$this->translate('category_search').': '.$categoriesSel[0]['name'].' <span id="getcatid'.$categoriesSel[0]['id'].'" class="setcatclose cyellow sh"></span></span>';
			    if(count($categoriesSel )>1) {
			        echo '&nbsp;&nbsp;<span id="expand" class="cyellow sh">&#9660;</span>';
			    }
			}
		?>
		</div>
		
		<div id="boxallcategories" style="z-index:9999; display:none;  width:300px; line-height:25px; background:#000000; padding:9px; border:1px solid #7b1a1c; position:absolute; right:10px; top:35px">
		<?php 
		    foreach($categoriesSel as $ind => $val)
		    {
		        if($ind==0) continue;
		        
		        echo '<p id="catclose'.$val['id'].'">'.$val['name'].' <span id="getcatid'.$val['id'].'"  class="setcatclose cyellow sh">['.$this->translate('off').']</span></p>';
		    }
		?>
		</div>
		
		<script type="text/javascript">
			$(document).ready(function(){

				var fsearch = $("#search_button").clone().css({ 
					display: "none", 
					position: "absolute", 
					zIndex: 10000, 
					top : "0px", 
					left: "0px",
					border : "1px solid #7B1A1C"
				}).attr({id : "fly_search"}).prependTo("body");

				fsearch.find("input").val( "ZAKTUALIZUJ" ).click(function(){
					$("#sfrom").submit();
				});

				$('#expand').click(function(){

					if($('#boxallcategories').is(':visible'))
					{
						$(this).html('&#9660;');
						$('#boxallcategories').slideUp();
					}
					else
					{
						$(this).html('&#9650;');
						$('#boxallcategories').slideDown();
					}
				});

				var allcat = <?php echo count($categoriesSel)-1 ?>;
				
				$('.setcatclose').click(function(){
				  var catid = $(this).attr('id');
				  catid = catid.slice(8);  
				  allcat = allcat - 1;

				  var bds = $("#boxallcategories").bounds();
				  fsearch.css({ display : "block" }).animate({
					  top : parseInt( bds.top - 20 ) + "px",
					  left : parseInt( bds.left + 203 ) + "px"
				  });

					/*
				  if(allcat==0) 
				  {
					  $('#boxallcategories').slideUp();
					  $('#expand').hide();
				  }
				  */
    				$('#catclose'+catid).fadeOut();	  
    				$('#c'+catid).attr('checked', false);
    
    				$('label[for="c'+catid+'"]').removeClass('ui-checkbox-checked');
    				$('label[for="c'+catid+'"]').removeClass('ui-checkbox-state-checked');

				 });
			
			});
		
		</script>
		
		<!-- end kategorie -->
	</div>
</div>
<?php } ?>

	<?php $i = 0; foreach( $list as $value ) { ?>

<h2><?php 
		echo $this->translate('search_res');
?></h2>
		<div class="content<?php echo ( $i > 0 ) ? ' mtop5' : '' ?>">
			<div class="box">
				<div class="column">
					
					<div class="colimg" style="background-image:url(<?php echo $this->File( $value['Galeries']['Photos']->toArray(), 195 ) ?>)">
						<a href="{baseUrl}/shop/item/id/<?php echo $value['p_id'] ?>/<?php echo $this->Seo( $value['name_' . $this->locale] ) ?>"></a>
					</div>
					
					<div class="coltext">
						<div class="prod">
							<div class="name"><a href="{baseUrl}/shop/item/id/<?php echo $value['p_id'] ?>/<?php echo $this->Seo( $value['name_' . $this->locale] ) ?>"><?php echo $value['name_' . $this->locale] ?></a><p class="price"><?php echo $this->Price( $value, $free, $this->locale, true ) ?></p></div>
							<div class="text">
							 <?php echo $value['description_'.$this->locale] ?>
							</div>
							<div class="avail">
								<?php 
								if($value['Dimension'][$free]['stock'] > 0) { ?>
									<img src="{baseUrl}/public/images/bgreen.png" alt=""><?php echo $this->translate( 'product_available' ) ?>
									<?php if( $value['wishlist'] == 'no' ) { ?>
										<a class="button" href="{baseUrl}/shop/basket/add/<?php echo $value['p_id'] ?>/dem/<?php echo $value['Dimension'][$free]['d_id'] ?>" ><?php echo $this->translate( 'add_to_basket' ) ?></a>
									<?php } ?>
								<?php } else {?>
									<img src="{baseUrl}/public/images/bred.png" alt=""><?php echo $this->translate( 'product_not_available' ) ?>
									<a class="button" href="{baseUrl}/shop/wishlist/id/<?php echo $value['p_id'] ?>" ><?php echo $this->translate( 'add_to_whishlist' ) ?></a>
								<?php }	?>
							</div>
						</div> 
					</div>
				<div class="clr"></div>
				</div>
				<div class="end"></div>
			</div>
			
			
		</div>
			

	<?php $i++; } ?>
<?php 
	if( $pager ) {
		echo $this->render('global/pager_service.phtml');
	} 
?>

<?php } ?>